package com.bobi.tfca.ui.mainnavigation.message.notification

import android.annotation.SuppressLint
import android.os.Bundle
import android.widget.ImageView
import android.widget.Toast
import androidx.navigation.findNavController
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bobi.tfca.R
import com.bobi.tfca.base.BaseFragment
import com.bobi.tfca.databinding.FragmentNotificationBinding
import com.bobi.tfca.network.client.AppCache
import com.bobi.tfca.network.response.MessageResponse
import com.bobi.tfca.ui.adapter.LetterAndNotificationAdapter

class NotificationFragment : BaseFragment<FragmentNotificationBinding, NotificationFragmentVM>(
    FragmentNotificationBinding::inflate,
    NotificationFragmentVM::class.java,
    true
) {
    // 通知RV
    private lateinit var notificationRV: RecyclerView
    private lateinit var noDataIV: ImageView

    @SuppressLint("ResourceType", "UseRequireInsteadOfGet")
    override fun initFragment(
        binding: FragmentNotificationBinding,
        viewModel: NotificationFragmentVM?,
        savedInstanceState: Bundle?
    ) {
        // 设置头部菜单的按钮
        setTopPanelStyle(binding)

        // 信件RecyclerView初始化
        initRecyclerView(binding)

        noDataIV = binding.ivNoData
        // 设置监听
        viewModel!!.apply {
            notifications.observe(this@NotificationFragment) {
                val adapter = notificationRV.adapter as LetterAndNotificationAdapter
                adapter.setItemList(it)
                adapter.notifyDataSetChanged()
            }
        }
    }

    /**
     * 设置顶部面板的样式
     */
    private fun setTopPanelStyle(binding: FragmentNotificationBinding) {
        val topPanel = binding.topPanel
        // 左边布局
        val leftImage = topPanel.leftImage
        leftImage.setOnClickListener{
            requireActivity().findNavController(R.id.app_nav).navigateUp()
        }

        // 右边布局
        val rightButton = topPanel.rightButton
        rightButton.text = "发布通知"
        rightButton.setOnClickListener {
            val role = AppCache.getMyInfo().role
            // 普通会员不能发布通知
            if ("会员".equals(role)) {
                Toast.makeText(requireActivity(), "请联系协会管理人员发布通知", Toast.LENGTH_SHORT).show()
            } else {
                requireActivity().findNavController(R.id.app_nav).navigate(R.id.action_notificationFragment_to_publishNotificationFragment)
            }
        }

        val title = topPanel.titleText
        // 中间标题
        title.text = "通知"
    }

    /**
     * 初始化RV
     */
    private fun initRecyclerView(
        binding: FragmentNotificationBinding
    ) {
        // 初始化
        notificationRV = binding.notificationRV
        val notificationList : ArrayList<MessageResponse> = arrayListOf()

        val linearLayoutManager = LinearLayoutManager(binding.root.context)
        linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
        notificationRV.layoutManager = linearLayoutManager
        // 添加分隔线
        notificationRV.addItemDecoration(DividerItemDecoration(binding.root.context, LinearLayoutManager.VERTICAL))
        // 绑定Adapter
        val notificationAdapter = LetterAndNotificationAdapter(this, notificationList)
        notificationAdapter.setOnItemClickListener(object : LetterAndNotificationAdapter.OnItemClickListener{
            override fun onItemClick(position: Int) {
                // 根据Int 跳转并设置对应的Fragment，携带参数
                val notification = notificationAdapter.list[position]
                publicViewModel!!.message.value = notification
                requireActivity().findNavController(R.id.app_nav).navigate(R.id.action_notificationFragment_to_notificationDetailFragment)
            }
        })
        notificationRV.adapter = notificationAdapter
    }

    override fun resumeFragment(viewModel: NotificationFragmentVM?) {
        viewModel!!.findAllNotifications(publicViewModel!!, this@NotificationFragment, noDataIV)
    }
}